//////////////////////////////////////////////////////////////////////////
//Created Date  : 2017/08/16
//FileName      : main.c
//Author        : tangm421@outlook.com
//Description   : 
//////////////////////////////////////////////////////////////////////////

#include "base64.h"
#include <time.h>
#include <stdio.h>

#define MAX_FILE_PATH   256

int main(int argc, char* argv[])
{
    char        *outfilepath;
    char        *filepath;
    FILE        *infile;
    FILE        *outfile;
    char        *in;
    size_t      insize;
    char        *out;
    size_t      outsize;
    long        size;
    Base64      bs64;

    char        *autofilepath;
    time_t      now;

    if (argc > 2) {
        filepath = argv[1];
        outfilepath = argv[2];
        autofilepath = NULL;
    } else if (argc > 1) {
        filepath = argv[1];
        outfilepath = NULL;
        autofilepath = NULL;
    } else {
        printf("usage: %s in_file_path [out_file_path]\n", argv[0]);
        return -1;
    }

    filepath = argv[1];

    infile = fopen(filepath, "r");
    if (!infile) {
        printf("can not open file '%s'\n", filepath);
        return -1;
    }

    if (0 != fseek(infile, 0, SEEK_END)) {
        printf("can not seek file '%s'\n", filepath);
        fclose(infile);
        return -1;
    }

    insize = ftell(infile);
    if (insize <= 0) {
        printf("infile size invalid!\n");
        fclose(infile);
        return -1;
    }

    printf("Base64 encode content size:%d(%.2fkb)\n", insize, insize / 1024.0);

    in = (char*)malloc(insize);
    if (!in) {
        printf("in malloc failed!\n");
        fclose(infile);
        return -1;
    }

    if (0 != fseek(infile, 0, SEEK_SET)) {
        printf("can not seek file '%s'\n", filepath);
        fclose(infile);
        return -1;
    }
    size = fread((void*)in, 1, insize, infile);

    if (size < insize) {
        printf("read not enought! size:%d\n", size);
        fclose(infile);
        return -1;
    }

    outsize = bs64.DecodedLength(in, insize);
    if (outsize <= 0) {
        printf("outfile size invalid!\n");
        free(in);
        return -1;
    }

    printf("Base64 decode content size:%d(%.2fkb)\n", outsize, outsize / 1024.0);

    out = (char*)malloc(outsize);
    if (!out) {
        printf("out malloc failed!\n");
        free(in);
        return -1;
    }

    if (!bs64.Decode(in, insize, out, outsize)) {
        printf("base64 decode failed\n");
        free(out);
        free(in);
        return -1;
    } else {
        free(in);
    }

    if (!outfilepath) {
        autofilepath = (char *)malloc(MAX_FILE_PATH);
        time(&now);
        strftime(autofilepath, MAX_FILE_PATH, "%Y%m%d_%H%M%S.png", localtime(&now));
        outfilepath = autofilepath;
    }

    outfile = fopen(outfilepath, "wb");
    if (!outfile) {
        printf("can not open file '%s'\n", filepath);
        if (autofilepath) {
            free(autofilepath);
        }
        free(out);
        return -1;
    }

    size = fwrite(out, 1, outsize, outfile);

    printf("Write to file '%s', size:%d\n", outfilepath, size);

    if (autofilepath) {
        free(autofilepath);
    }
    fclose(outfile);
    free(out);

    return 0;
}
